package sanrenxing.foreStudentGrade.dao.impl;

import sanrenxing.foreStudentGrade.dao.StudentGradeDao;
import sanrenxing.foreStudentGrade.domain.StudentGrade;
import sanrenxing.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2020/10/14 19:17
 * 编辑者：邓志强
 * 功能：
 */
public class StudentGradeDaoImpl implements StudentGradeDao {
    /**
     * 查询成绩
     *
     * @param stuId
     * @return
     */
    @Override
    public List<StudentGrade> findGrade(String stuId) {
        List<StudentGrade> list=new ArrayList<>();
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            conn= JdbcUtils.getConnection();
            String sql= "SELECT s.scoreId scoreId,c.courseName courseName,s.grades grades,p.examDate examDate FROM" +
                    " score s JOIN course c ON s.courseId=c.courseId JOIN paper p ON p.paperId=s.paperId WHERE stuId=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,stuId);
            rs=pstmt.executeQuery();
            while (rs.next()){
                StudentGrade studentGrade=new StudentGrade();
                studentGrade.setScoreId(rs.getInt("scoreId"));
                studentGrade.setCourseName(rs.getString("courseName"));
                studentGrade.setGrades(rs.getString("grades"));
                studentGrade.setExamDate(rs.getString("examDate"));
                list.add(studentGrade);
            }
        } catch (SQLException e) {
            new RuntimeException(e.getMessage(),e);
        }finally{
            JdbcUtils.close(rs,pstmt,conn);
        }
        return list;
    }
}
